package cn.shop.front.service.impl;

import cn.shop.front.service.InboxService;
import cn.shop.transcation.inbox.mapper.InboxMapper;
import cn.shop.transcation.inbox.model.Inbox;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2022-3-30 11:35
 * @description :
 */
@Service("inboxService")
public class InboxServiceImpl extends ServiceImpl<InboxMapper, Inbox> implements InboxService {

	@Override
	public boolean batchDelete(List<Long> idList, Inbox inbox) {
		return false;
	}

	@Override
	public boolean deleteWithFill(Inbox inbox, Wrapper<Inbox> wrapper) {
		return false;
	}

	@Override
	public long getUnreadCount(long uid) {
		return count(Wrappers.<Inbox>lambdaQuery()
				.eq(Inbox::getUserId, uid)
				.eq(Inbox::isRead, false));
	}

	@Override
	public List<Map<String, Object>> inboxList(long uid) {
		List<Inbox> list = list(Wrappers.<Inbox>lambdaQuery()
				.eq(Inbox::getUserId, uid)
				.orderByDesc(Inbox::getCreateTime));
		return list.stream().map(this::formant).collect(Collectors.toList());
	}

	@Override
	public boolean read(List<Long> ids) {
		Inbox update = new Inbox();
		update.setRead(true);
		return update(update, Wrappers.<Inbox>lambdaQuery().in(Inbox::getId, ids));
	}

	private Map<String, Object> formant(Inbox inbox) {
		return Putif.start()
				.anyway("title", inbox.getTitle())
				.anyway("content", inbox.getContent())
				.anyway("time", inbox.getCreateTime())
				.anyway("id", inbox.getId())
				.get();
	}

}
